package com.yihuier.yihuier.Controller.Common;

import com.yihuier.yihuier.Service.MeetingManager.MeetingAttendService;
import com.yihuier.yihuier.Service.MeetingManager.MeetingRoomService;
import com.yihuier.yihuier.Service.MeetingManager.MeetingService;
import com.yihuier.yihuier.Service.RoomManager.CompanyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 为会议，公司的时间期限问题更新状态
 */
@RestController
public class TimeJudgeController {

    @Autowired
    CompanyService companyService;

    @Autowired
    MeetingService meetingService;

    @Autowired
    MeetingRoomService meetingRoomService;

    @Autowired
    MeetingAttendService meetingAttendService;

    /**
     * 判断公司服务时间期限并且更新状态
     */
    public boolean updateCompanyStatus(int company_id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设定时间格式规范
        Date companyDeadline = companyService.getCompanyDeadline(company_id);
        try {
            Date company_server_time = sdf.parse(sdf.format(companyDeadline));//公司服务时间
            Date now_time = new Date();//现在时间
            if (company_server_time.getTime() < now_time.getTime()) {//如果公司服务时间到期
                int updateStatus = companyService.updateCompanyStatus(company_id, 0);//将公司状态置为不可用
                return updateStatus == 1;
            } else {
                int updateStatus = companyService.updateCompanyStatus(company_id, 1);//将公司状态置为可用
                return updateStatus == 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断会议时间期限和人员并且更新状态
     */
    public int updateMeetingAndPersonStatus(int meeting_id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设定时间格式规范
        Map<String, Object> MeetingInfo = meetingService.getMeetingInfoByMeetingId(meeting_id);
        if ((Integer)MeetingInfo.get("meeting_status") == 3 || (Integer)MeetingInfo.get("meeting_status") == 4 ){//如果已经被处理过，就不再处理
            return 1;
        }
        String tempObj = "" + meeting_id;
        Map<String, Object> meeting_room_info = meetingRoomService.getMeetingTimes((Long) MeetingInfo.get("location"), Long.parseLong(tempObj));
        if (meeting_room_info == null){
            return 2;
        }
        try {
            Date meeting_start_time = sdf.parse(sdf.format(meeting_room_info.get("meeting_time_start")));//会议开始时间
            Date now_time = new Date();//现在时间
            String tempObj2 = "" + meeting_room_info.get("meeting_time");
            if ((now_time.getTime() - meeting_start_time.getTime()>= 0)) {//如果现在时刻大于开会时间,开会已经开始
                if (((now_time.getTime() - meeting_start_time.getTime()) / 1000 / 60 - Integer.parseInt(tempObj2)) < 0) {//正在开会
                    int updateResult = meetingService.updateMeetingStatus(meeting_id, 2);//将会议状态置为2正在开会
                    return 3;
                } else {
                    int updateResult = meetingService.updateMeetingStatus(meeting_id, 3);// 将会议状态置为3已结束
                    if (updateResult == 1){
                        int finalResult = meetingAttendService.updateAttendence(meeting_id);//将参加该会的所有4未到的人的状态置为3缺勤
                        return 4;
                    }
                    return 5;
                }
            } else {//如果现在时刻小于开会时间,会议还未开始
                if (((now_time.getTime() - meeting_start_time.getTime()) / 1000 / 60 + 15 > 0)) {//会议即将开始
                    int updateResult = meetingService.updateMeetingStatus(meeting_id, 1);//将会议状态置为1即将开会
                    return 6;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 7;
    }

}
